package com.fs.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fs.dao.ProductDao;
import com.fs.dao.UserInfoDao;
import com.fs.entity.Product;
import com.fs.entity.UserInfo;
import com.fs.utils.CookieEncrypTool;

@WebServlet("/login.do")
public class UserLoginServlet extends HttpServlet {
	
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
				doPost(req, resp);
		
		}
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			req.setCharacterEncoding("utf-8");//设置请求编码格式
			resp.setContentType("text/html;charset=utf-8");//设置响应的编码格式
			resp.setCharacterEncoding("utf-8");
			String type =req.getParameter("type");
			PrintWriter out = resp.getWriter();
			//String user_name = req.getParameter("user_name");
		
			String user_email = req.getParameter("user_email");
			String user_password = req.getParameter("user_password");
			String rememberMe = req.getParameter("rememberMe");
			
			UserInfoDao userDao = new UserInfoDao();
			ProductDao prodao=new ProductDao();
			//第一次请求的时候，服务端会自动产生一个sessionID返回给客户端，客户端利用cookie技术将其保存在浏览器
			HttpSession session = req.getSession();
			String sessionId  = session.getId();
			UserInfo userInfo = userDao.login(user_email, user_password);//返回登录信息
			
			if("spQuery".equals(type)){
				List<Product> prolist=prodao.getProductListAll();
				req.setAttribute("prolist",prolist );
				req.getRequestDispatcher("index.jsp").forward(req, resp);
			}
			
			if(userInfo.getUser_id()!=0){
				//表示通过验证，登录成功
				//session.setAttribute("user_name", user_name);//把用户信息存在
				session.setAttribute("user_id", userInfo.getUser_id());
				session.setAttribute("user_email", user_email);
				session.setAttribute("user_password", user_password);
				session.setAttribute("userInfo", userInfo);
				//取到type值
				String usertype =userInfo.getType();
				session.setAttribute("usertype", usertype);//把usertype（用户类型）存在session中，以便判断
				
				rememberMe(rememberMe,user_email,user_password,req,resp);
				List<Product> prolist=prodao.getProductListAll();
				req.setAttribute("prolist",prolist );
				//req.getRequestDispatcher("index.jsp").forward(req, resp);
				
				
				if("0".equals(usertype)){
					rememberMe(rememberMe,user_email,user_password,req,resp);
					//resp.sendRedirect("index.jsp");//"个人用户" 登录成功之后跳转到index页面
					req.getRequestDispatcher("index.jsp").forward(req, resp);
				}else{
					resp.sendRedirect("personal/index.jsp");//"卖家用户" 登录成功之后跳转到后台管理页面
				}
				
				
				
				/*resp.sendRedirect("index.jsp");//登录成功之后跳转到inde页面
*/			}else{
				out.print("<script type = 'text/javascript'>");
				out.print("alert('用户名或者密码错误，请重新登录');");
				out.print("window.location.href='Login.jsp'");
				out.print("</script>");
				//如果登录验证失败，则跳转页面
				/*resp.sendRedirect("login.jsp");*/
			}
		}
		private void rememberMe(String rememberMe,
				String user_email, String user_password,
				HttpServletRequest req, HttpServletResponse resp) {
					if("true".equals(rememberMe)){
						Cookie cookie = new Cookie("Cookie_username", CookieEncrypTool.encodeBase64(user_email));
						cookie.setPath("/");//设置cookie的保存路径
						cookie.setMaxAge(7*24*3600);//设置cookie的保存时间
						resp.addCookie(cookie);//将cookie保存在浏览器
						
						/*cookie  = new Cookie("Cookie_email", CookieEncrypTool.encodeBase64(user_email));
						cookie.setPath("/");//设置cookie的保存路径
						cookie.setMaxAge(7*24*3600);//设置cookie的保存时间
						resp.addCookie(cookie);//将cookie保存在浏览器
*/						
						
						cookie = new Cookie("Cookie_password", CookieEncrypTool.encodeBase64(user_password));
						cookie.setPath("/");//设置cookie的保存路径
						cookie.setMaxAge(7*24*3600);//设置cookie的保存时间
						resp.addCookie(cookie);//将cookie保存在浏览器
						
					}else{
						//将邮箱和密码清空
						//得到cookie的值
						Cookie[] cookies  = req.getCookies();//获取所有的cookie数据
						if(cookies!=null){
							for(Cookie cookie:cookies){
								if("Cookie_username".equals(cookie.getName())||"Cookie_password".equals(cookie.getName())){
									cookie.setMaxAge(0);//删除cookie值
									cookie.setPath("/");
									resp.addCookie(cookie);
								}
							}
						}
					}
				}
			}
